package com.wrl.mmall.controller.portal;

import com.wrl.mmall.common.ResponseCode;
import com.wrl.mmall.common.ServerResponse;
import com.wrl.mmall.pojo.Shipping;
import com.wrl.mmall.pojo.User;
import com.wrl.mmall.service.IShippingService;
import com.wrl.mmall.util.CookieUtil;
import com.wrl.mmall.util.JsonUtil;
import com.wrl.mmall.util.RedisShardedPoolUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 收获地址管理
 * Created by Wang Renlei on 2018/9/20.
 */
@Slf4j
@Controller
@RequestMapping("/shipping")
public class ShippingController {


    @Autowired
    private IShippingService iShippingService;


    /**
     * 新增收货地址
     *
     * @param httpServletRequest
     * @param shipping 用户填写的收获地址详情
     */
    @RequestMapping(value = "add.do")
    @ResponseBody
    public ServerResponse add(HttpServletRequest httpServletRequest, Shipping shipping) {
        log.info("ShippingController.add");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.add(user.getId(), shipping);
    }

    /**
     * 删除收货地址
     *
     * @param shippingId
     */
    @RequestMapping(value = "delete.do")
    @ResponseBody
    public ServerResponse delete(HttpServletRequest httpServletRequest, Integer shippingId) {

        log.info("ShippingController.delete");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.delete(user.getId(), shippingId);
    }

    /**
     * 修改收货地址
     *
     * @param shipping 用户修改后的收获地址详情
     */
    @RequestMapping(value = "update.do")
    @ResponseBody
    public ServerResponse update(HttpServletRequest httpServletRequest, Shipping shipping) {

        log.info("ShippingController.update");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.update(user.getId(), shipping);
    }

    /**
     * 查看收货地址详情
     *
     * @param shippingId 用户填写的收获地址详情
     */
    @RequestMapping(value = "select.do")
    @ResponseBody
    public ServerResponse<Shipping> select(HttpServletRequest httpServletRequest, Integer shippingId) {

        log.info("ShippingController.select");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.select(user.getId(), shippingId);
    }

    /**
     * 查看所有收货地址
     *
     */
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                         HttpServletRequest httpServletRequest) {

        log.info("ShippingController.list");
        String loginToken = CookieUtil.readLoginToken(httpServletRequest);
        if (StringUtils.isEmpty(loginToken)) {
            return ServerResponse.createByErrorMessage("用户未登录，无法获取信息");
        }
        String userStr = RedisShardedPoolUtil.get(loginToken);
        User user = JsonUtil.stringToObj(userStr, User.class);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }

        return iShippingService.list(user.getId(), pageNum, pageSize);
    }


}
